package com.up.vms.interfaces.rest.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import com.up.vms.application.service.SystemUserService;
import com.up.vms.domain.module.UserDTO;
import com.up.vms.domain.vm.enums.AdminRespCode;
import com.up.vms.infrastructure.mvc.GeneRespDTO;

/**
 * <p>
 * 获取用户详情(包含角色以及菜单）
 * </p>
 *
 */
public class UserContextServiceImpl implements UserDetailsService {

    @Autowired
    private SystemUserService systemUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        // 通过用户名查看用户相关信息
        GeneRespDTO<UserDTO> generalServiceResp = systemUserService.getUserModelByUsername(username);
        if (null == generalServiceResp || null == generalServiceResp.getData()) {
            throw new UsernameNotFoundException(AdminRespCode.FIND_NO_USER.msg);
        }

        return new UserContext(generalServiceResp.getData());
    }
}
